package com.example.admin.controller.statistics;

import com.example.airobotstatistics.dto.InterviewStatisticsDto;
import com.example.airobotstatistics.service.InterviewStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "面试统计接口")
@RestController
@RequestMapping("/statistics/interview")
public class InterviewStatisticsController {

    @Autowired
    private InterviewStatisticsService interviewStatisticsService;

    @GetMapping("/{positionId}")
    @ApiOperation("获取面试统计数据")
    public ResponseEntity<Map<String, Object>> getInterviewStatistics(@PathVariable Integer positionId) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            InterviewStatisticsDto statistics = interviewStatisticsService.getInterviewStatistics(positionId);
            
            // 计算通过率
            if (statistics != null && statistics.getTotalInterviews() != null && statistics.getTotalInterviews() > 0) {
                double passRate = (double) statistics.getPassedInterviews() / statistics.getTotalInterviews() * 100;
                statistics.setPassRate(Math.round(passRate * 100.0) / 100.0);
            } else if (statistics != null) {
                statistics.setPassRate(0.0);
            }
            
            response.put("code", 200);
            response.put("msg", "获取成功");
            response.put("data", statistics);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            response.put("code", 500);
            response.put("msg", "获取统计数据失败: " + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
} 